package com.tyjc.prizeOptimize;

import java.math.BigDecimal;
import java.math.RoundingMode;

//标准差计算
public class StandardDeviation {


	private BigDecimal[] samples;


	private BigDecimal getAve() {
		BigDecimal total = BigDecimal.ZERO;
		for(BigDecimal b:samples) {
			total = total.add(b);
		}
		return total.divide(new BigDecimal(samples.length),4,RoundingMode.HALF_UP);
	}



	public double calStandardDeviation() {
		Double s = 0d;
		BigDecimal ave = getAve();
		for(BigDecimal b:samples) {
			s = s + Math.pow(b.doubleValue()-ave.doubleValue(), 2);
		}
		return Math.sqrt(s/samples.length);
	}


	public StandardDeviation(BigDecimal[] samples) {
		super();
		this.samples = samples;
	}

}
